package com.aqie.demo.Face;
import com.aqie.demo.HIKNetSDK.HCNetSDK;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.io.IOException;
import java.util.Iterator;
/**
 * @author jiangxin
 * @create 2021-04-14-14:55
 * 人脸库以图搜图模块，功能：通过人脸图片检索设备人脸库中达到相似阈值的人脸图片 ,调用顺序：先上传人脸图片进行建模，获取建模数据，使用建模数据在人脸库中查询图片
 */
public class FDSearch {
    static String ModeData; //图片建模数据
    ISAPI isapi = new ISAPI();
    //上传人脸图片，获取建模数据
    public String analysisImage(int lUserID) throws IOException, DocumentException {
        HCNetSDK.NET_DVR_STRING_POINTER stringRequest = new HCNetSDK.NET_DVR_STRING_POINTER(1024);
        stringRequest.read();
        stringRequest.byString = "POST /ISAPI/Intelligent/analysisImage/face".getBytes();
        stringRequest.write();
        //读取二进制文件
        byte[] picbyte = FacePicManage.toByteArray("E:\\2.Demo汇总\\JAVA_DEMO\\01-Windows_Demo\\超脑人脸比对\\Test\\pic\\FDLib.jpg");
        HCNetSDK.BYTE_ARRAY byte_array = new HCNetSDK.BYTE_ARRAY(picbyte.length);
        byte_array.read();
        byte_array.byValue = picbyte;
        byte_array.write();
        HCNetSDK.NET_DVR_XML_CONFIG_INPUT struXMLInput = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
        struXMLInput.read();
        struXMLInput.dwSize = struXMLInput.size();
        struXMLInput.lpRequestUrl = stringRequest.getPointer();
        struXMLInput.dwRequestUrlLen = stringRequest.byString.length;
        struXMLInput.lpInBuffer = byte_array.getPointer();
        struXMLInput.dwInBufferSize = byte_array.size();
        struXMLInput.write();
        HCNetSDK.NET_DVR_STRING_POINTER stringXMLOut = new HCNetSDK.NET_DVR_STRING_POINTER(8 * 1024);
        stringXMLOut.read();
        HCNetSDK.NET_DVR_STRING_POINTER struXMLStatus = new HCNetSDK.NET_DVR_STRING_POINTER(1024);
        struXMLStatus.read();
        HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
        struXMLOutput.read();
        struXMLOutput.dwSize = struXMLOutput.size();
        struXMLOutput.lpOutBuffer = stringXMLOut.getPointer();
        struXMLOutput.dwOutBufferSize = stringXMLOut.size();
        struXMLOutput.lpStatusBuffer = struXMLStatus.getPointer();
        struXMLOutput.dwStatusSize = struXMLStatus.size();
        struXMLOutput.write();
        if (!FaceMain.hCNetSDK.NET_DVR_STDXMLConfig(lUserID, struXMLInput, struXMLOutput)) {
            int iErr = FaceMain.hCNetSDK.NET_DVR_GetLastError();
            System.err.println("NET_DVR_STDXMLConfig失败，错误号" + iErr);
            return null;
        } else {
            stringXMLOut.read();
            System.out.println("输出文本大小：" + struXMLOutput.dwReturnedXMLSize);
            //打印输出XML文本
            String strOutXML = new String(stringXMLOut.byString).trim();
            System.out.println(strOutXML);
            Document doc = null;
            doc = DocumentHelper.parseText(strOutXML); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            Iterator iter = rootElt.elementIterator("FaceContrastTarget"); // 获取根节点下的子节点FaceContrastTarget
            while (iter.hasNext()) {
                Element recordEle = (Element) iter.next();
                ModeData = recordEle.elementTextTrim("modeData"); // 拿到FaceContrastTarget节点下的子节点modeData值
                System.out.println("modeData:" + ModeData);
            }
            struXMLStatus.read();
            String strStatus = new String(struXMLStatus.byString).trim();
            System.out.println(strStatus);
            return ModeData;
        }
    }
    //查询人脸比对库里的图片数据
    public void faceLibSearch(int lUserID) {
        //POST /ISAPI/Intelligent/FDLib/FDSearch
        isapi.Isapi(lUserID, "POST /ISAPI/Intelligent/FDLib/FDSearch", XmlCreat());
    }
    //  查询人脸比对库里的图片输入XML报文
    public static String XmlCreat() {
        Document document1;
        Element root = DocumentHelper.createElement("FDSearchDescription");
        document1 = DocumentHelper.createDocument(root);
        Element searchID = root.addElement("searchID");
        searchID.setText("C929433A-AD10-0001-CA62-1A701E0015F4");
        Element searchResultPosition = root.addElement("searchResultPosition");
        searchResultPosition.setText("0");
        Element maxResults = root.addElement("maxResults");
        maxResults.setText("50");
        Element FDID = root.addElement("FDID");
        FDID.setText("2CC0120275C04A5393BB5EED9FAE75E1");
        Element FaceModeList = root.addElement("FaceModeList");
        Element FaceMode = FaceModeList.addElement("FaceMode");
        Element ModeInfo = FaceMode.addElement("ModeInfo");
        Element similarity = ModeInfo.addElement("similarity");
        similarity.setText("80");
        Element modeData = ModeInfo.addElement("modeData");
        modeData.setText(ModeData);
        String requestXml = document1.asXML();
        System.out.println(requestXml);
        return requestXml;
    }
}
